package cn.com.dhc.danlu.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.text.TextUtils;

import java.math.BigDecimal;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.LoginActivity;
import cn.com.dhc.danlu.activity.myself.MyAccountActivity;
import cn.com.dhc.danlu.widget.dialog.ViewDialog.Builder;
import cn.com.hd.mall.web.webservices.entity.response.login.LoginResponse;
import cn.com.hd.mall.web.webservices.entity.response.login.LoginResponse.SysParm;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * @author wuhaiyang
 */
public class ActivePhoneProcess {
    public static boolean isShowBefore = false;
    private boolean isSwitchActiveFragment = false;
    private IOnCancelActive mCallback;

    public void setSwitchFragmentStatus (boolean isSwitch) {
        this.isSwitchActiveFragment = isSwitch;
    }

    public void showActiveDialog (final Context context, final boolean isFromLogin) {
        if (isShowBefore) {
            return;
        }
        final LoginResponse response = agent.getLoginUser ();
        String actForceInd;
        SysParm sysParm = response.getSysParm ();
        String content = context.getResources ().getString (R.string.active_tips);
        if (null == sysParm) {
            return;
        } else {
            actForceInd = sysParm.getActForceInd ();
        }

        if (null != sysParm) {
            String amtRes = sysParm.getActCouponAmt ();
            if (TextUtils.isEmpty (response.getFirstActiveTime ()) && !TextUtils.isEmpty (amtRes) && !amtRes.equals ("0")) {
                //首次激活 切活动未结束
                String amt = CommonUtils.fenToYuan (new BigDecimal (amtRes)).toPlainString ();
                content += "，激活成功后可获得" + amt + "元红包！";
            }
        }
        Builder builder = new Builder (context)
                .setCancelable (false)
//                .setPositiveButtonDrawable (context.getResources ().getDrawable (R.drawable.btn_dialog_cancel))
//                .setNegativeButtonDrawable (context.getResources ().getDrawable (R.drawable.btn_dialog_confirm))
                .setMessage (content);
        if (actForceInd.equals ("0")) {
            //强制激活
            builder.setNegativeButton (R.string.active_now, new OnClickListener () {
                @Override
                public void onClick (DialogInterface dialog, int which) {
                    dialog.dismiss ();
                    //跳转到个人账户界面去激活手机号码
                    skipActiveIntent (context, true, isFromLogin);
                    if (null != mCallback) {
                        mCallback.confirmActive ();
                    }
                }
            });
            builder.setPositiveButton (R.string.exit_login, new OnClickListener () {
                @Override
                public void onClick (DialogInterface dialog, int which) {
                    dialog.dismiss ();
                    if (!(context instanceof LoginActivity)) {
                        CommonUtils.exitCleanAction (context);
                    }
                }
            });
        } else {
            //未强制激活
            builder.setPositiveButton (R.string.active_after, new OnClickListener () {
                @Override
                public void onClick (DialogInterface dialog, int which) {
                    dialog.dismiss ();
                    isShowBefore = true;
                    //取消激活
                    if (null != mCallback) {
                        mCallback.cancelActive ();
                    }
                }
            }).setNegativeButton (R.string.active_now, new OnClickListener () {
                @Override
                public void onClick (DialogInterface dialog, int which) {
                    dialog.dismiss ();
                    //跳转到个人账户界面去激活手机号码
                    skipActiveIntent (context, false, isFromLogin);
                    if (null != mCallback) {
                        mCallback.confirmActive ();
                    }
                }
            });
        }
        if (!((Activity) context).isFinishing ()) {
            builder.create ().show ();
        }
    }

    private void skipActiveIntent (Context context, boolean isForceActive, boolean isFromLogin) {
        Intent intent = new Intent (context, MyAccountActivity.class);
        intent.putExtra ("isFromLogin", isFromLogin);
        intent.putExtra ("isForceActive", isForceActive);
        intent.putExtra ("isSwitchActiveFragment", isSwitchActiveFragment);
        context.startActivity (intent);
    }

    public void setOnCancelActiveListener (IOnCancelActive listener) {
        this.mCallback = listener;
    }

    public interface IOnCancelActive {
        void cancelActive ();

        void confirmActive ();
    }
}